package com.runrunfast.fxj.config.shiro;

import com.runrunfast.fxj.utils.PasswordUtil;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: 陈胤训
 * @create: 2019-03-13 11:55
 * Shiro-密码输入错误的状态下重试次数的匹配管理
 **/
public class RetryLimitCredentialsMatcher extends SimpleCredentialsMatcher {

    public static final Logger LOGGER= LoggerFactory.getLogger(RetryLimitCredentialsMatcher.class);

    public static final String SHIRO_LOGIN_COUNT="shiro_login_count_";

    public static final String SHIRO_IS_LOCK="shiro_is_lock_";

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken utoken = (UsernamePasswordToken) token;

        String inPassword = new String(utoken.getPassword());
        String dbPassword = (String) info.getCredentials();
        try {
            System.out.println("数据库的密码为"+dbPassword+",账号为"+utoken.getUsername()+"加密后的密码："+PasswordUtil.encrypt(inPassword,utoken.getUsername()) );
//            dbPassword = PasswordUtil.decrypt(dbPassword, utoken.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return this.equals(inPassword, dbPassword);
    }
}